package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿相关接口")
@Slf4j
public class AddBookController {

    @Autowired
    private AddBookService addBookService;

    /**
     * 获取用户的地址簿
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取用户的地址簿")
    public Result<List<AddressBook>> BookList(){
        List<AddressBook> bookList = addBookService.getAll();
        return Result.success(bookList);
    }

    /**
     * 新增地址簿
     * @param addressBook
     * @return
     */
    @PostMapping()
    @ApiOperation("新增地址簿")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增地址簿:{}",addressBook);
        addBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据Id查询地址簿
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(("根据Id查询地址簿"))
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("地址簿Id:{}",id);
        AddressBook addressBook = addBookService.getById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址簿
     * @param addressBook
     * @return
     */
    @PutMapping()
    @ApiOperation("修改地址簿")
    public Result update(@RequestBody AddressBook addressBook){
        log.info("修改地址簿:{}",addressBook);
        addBookService.update(addressBook);
        return Result.success();
    }

    /**
     * 根据Id删除地址簿
     * @return
     */
    @DeleteMapping()
    @ApiOperation("根据Id删除地址簿")
    public Result deleteById(Long id){
        log.info("id:{}",id);
        addBookService.deleteById(id);
        return Result.success();
    }


    /**
     * 查询默认地址簿
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址簿")
    public Result<AddressBook> getDefault(){
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addBookService.getDefault(addressBook);

        if(list != null && list.size() == 1){
            return Result.success(list.get(0));
        }

        return Result.error(MessageConstant.DEFAULT_NOT_FOUND);
    }

    /**
     * 设置默认地址簿
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址簿")
    public Result setDefault(@RequestBody AddressBook addressBook){
        log.info("设置默认值Id:{}",addressBook);
        addBookService.setDefault(addressBook);
        return Result.success();
    }
}
